package com.learn.demo.config;

import com.learn.demo.enums.WebSocketMessageTypeEnum;
import com.learn.demo.listener.ChatMessageListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * webSocket消息订阅配置类
 *
 * @author banjiawei
 * @date 2021/05/11
 */
@Configuration
@Slf4j
public class RedisSubscriberConfig {

    /**
     * 监听的db配置
     */
    @Autowired
    @Qualifier("jedisConnectionFactory15")
    private RedisConnectionFactory redisConnectionFactory15;

    /**
     * 消息监听适配器，注入接受消息方法
     *
     * @param receiver
     * @return
     */
    @Bean(name = "messageListenerAdapter15")
    public MessageListenerAdapter messageListenerAdapter15(ChatMessageListener receiver) {
        return new MessageListenerAdapter(receiver);
    }

    /**
     * 连接事件消息监听容器
     * @author banjiawei
     * @date 2021/5/11
     * @param
     * @return org.springframework.data.redis.listener.RedisMessageListenerContainer
     */
    @Bean(name = "redisMessageListenerContainer15ForConnection")
    RedisMessageListenerContainer redisMessageListenerContainer15ForConnection(@Qualifier("messageListenerAdapter15") MessageListenerAdapter messageListenerAdapter15) {
        return getRedisMessageListenerContainer(messageListenerAdapter15, WebSocketMessageTypeEnum.ON_OPEN.getTopName());
    }

    /**
     * 关闭连接事件消息监听容器
     * @author banjiawei
     * @date 2021/5/11
     * @param
     * @return org.springframework.data.redis.listener.RedisMessageListenerContainer
     */
    @Bean(name = "redisMessageListenerContainer15ForDisconnection")
    RedisMessageListenerContainer redisMessageListenerContainer15ForDisconnection(@Qualifier("messageListenerAdapter15") MessageListenerAdapter messageListenerAdapter15) {
        return getRedisMessageListenerContainer(messageListenerAdapter15, WebSocketMessageTypeEnum.ON_CLOSE.getTopName());
    }

    /**
     * 发生错误事件消息监听容器
     * @author banjiawei
     * @date 2021/5/11
     * @param
     * @return org.springframework.data.redis.listener.RedisMessageListenerContainer
     */
    @Bean(name = "redisMessageListenerContainer15ForError")
    RedisMessageListenerContainer redisMessageListenerContainer15ForError(@Qualifier("messageListenerAdapter15") MessageListenerAdapter messageListenerAdapter15) {
        return getRedisMessageListenerContainer(messageListenerAdapter15, WebSocketMessageTypeEnum.ON_ERROR.getTopName());
    }

    /**
     * 传输消息事件消息监听容器
     * @author banjiawei
     * @date 2021/5/11
     * @param
     * @return org.springframework.data.redis.listener.RedisMessageListenerContainer
     */
    @Bean(name = "redisMessageListenerContainer15ForMessage")
    RedisMessageListenerContainer redisMessageListenerContainer15ForMessage(@Qualifier("messageListenerAdapter15") MessageListenerAdapter messageListenerAdapter15) {
        return getRedisMessageListenerContainer(messageListenerAdapter15, WebSocketMessageTypeEnum.ON_MESSAGE.getTopName());
    }
    /**
     * 监听不同的频道
     * @param messageListenerAdapter15
     * @param topicName
     * @return
     */
    private RedisMessageListenerContainer getRedisMessageListenerContainer(@Qualifier("messageListenerAdapter15") MessageListenerAdapter messageListenerAdapter15, String topicName) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory15);
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter15, new PatternTopic(topicName));
        return redisMessageListenerContainer;
    }
}
